<%@ include file="/common/taglibs.jsp"%>

<head>
    <title><fmt:message key="loanRequestList.title"/></title>
    <meta name="menu" content="LoanRequestListMenu"/>
</head>

<security:authorize ifAnyGranted="ROLE_DEALER_USER">
    <c:set var="availableRequestStatus" value="${availableDealerRequestStatus}"/>
</security:authorize>
<security:authorize ifNotGranted="ROLE_DEALER_ADMIN,ROLE_DEALER_USER">
    <c:set var="availableRequestStatus" value="${availableLoanRequestStatus}"/>
</security:authorize>

<c:if test="{'$'}{not empty searchError}">
    <div class="alert alert-danger alert-dismissable">
        <a href="#" data-dismiss="alert" class="close">&times;</a>
        <c:out value="{'$'}{searchError}"/>
    </div>
</c:if>

<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        <fmt:message key="loanRequestList.heading"/>
        <small><p><fmt:message key="loanRequestList.message"/></p></small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i><fmt:message key="loanRequestList.heading"/></a></li>
        <li class="active"> <fmt:message key="loanRequestList.heading"/></li>
    </ol>
</section>
<!-- Main content -->
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <%@ include file="/common/messages.jsp" %>
            <div class="box">
                <div class="box-header">
                    <div id="actions" class="btn-group margin">
                        <form:form commandName="searchForm" method="get" action="${ctx}/policyRequests" id="queryForm">
                            <div>
                                <%--<label><fmt:message key="loanRequest.number"/>: </label>
                                <form:input path="number" type="text" size="20" name="date" id="number"
                                            placeholder=""  cssClass="input-sm" style="width: 150px;"/>
                                <label><fmt:message key="loanRequest.status"/>: </label>
                                <form:select path="status" cssClass="input-sm" name="status" id="status">
                                    <option value="all"><fmt:message key="REQUEST_STATUS_all"/></option>
                                    <c:forEach items="${availableRequestStatus}" var="reqStatus">
                                        <option value="${reqStatus.value}" <c:if test="${reqStatus.value == param.status}">selected</c:if>><c:out value="${reqStatus.label}"/></option>
                                    </c:forEach>
                                </form:select>--%>
                                <label><fmt:message key="loanRequest.requestDate"/>: </label>
                                <form:input path="date" type="text" size="20" name="date" id="date"
                                       placeholder=""  cssClass="datepicker input-sm" style="width: 150px;"/>
                                <label><fmt:message key="loanPerson.name"/>: </label>
                                <form:input path="name" type="text" size="20" name="name" id="name"
                                            placeholder=""  cssClass="input-sm" style="width: 150px;"/>
                                <label><fmt:message key="loanRequest.salesman"/>: </label>
                                <form:input path="salesman" type="text" size="20" name="salesman" id="salesman"
                                            placeholder=""  cssClass="input-sm" style="width: 150px;"/>
                                <button id="button.search" class="btn btn-primary btn-sm" type="submit">
                                    <fmt:message key="button.search"/>
                                </button>
                            </div>
                        </form:form>
                    </div>
                    <!--div class="box-tools">
                        <form method="get" action="${ctx}/loanRequests" id="searchForm">
                            <div class="input-group">
                                <input type="text" size="20" name="q" id="query" value="${param.q}"
                                       placeholder="<fmt:message key="search.enterTerms"/>"  class="form-control input-sm pull-right" style="width: 150px;"/>
                                <div class="input-group-btn">
                                    <button id="button.gsearch" class="btn btn-default btn-sm" type="submit">
                                        <fmt:message key="button.search"/>
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div-->
                </div><!-- /.box-header -->
<div class="box-body">
                    <display:table name="loanRequestList" class="table table-bordered table-hover" requestURI="" id="loanRequestList" export="false" pagesize="25">
                        <display:column property="number" sortable="true" href="loanRequestform" media="html" style="width: 8%;"
                                        paramId="reqId" paramProperty="id" titleKey="loanRequest.number"/>
                        <display:column class="requestId" property="id" media="csv excel xml pdf" titleKey="loanRequest.id"/>
                        <display:column property="requestPerson.name" sortable="true" titleKey="loanRequest.requestUser" style="width: 7%;"/>
                        <display:column property="dealer.name" sortable="true" titleKey="loanRequest.dealer" style="width: 8%;"/>
                        <display:column property="dealerUser.username" sortable="true" titleKey="loanRequest.dealerUser" style="width: 8%;"/>
                        <display:column sortable="true" titleKey="loanRequest.idType" style="width: 8%;"><fmt:message key="ID_TYPE_${loanRequestList.requestPerson.idType}"/></display:column>
                        <display:column property="requestPerson.idNo" sortable="true" titleKey="loanRequest.idNo" style="width: 9%;"/>
                        <display:column sortable="true" titleKey="loanRequest.status" style="width: 8%;"><fmt:message key="REQUEST_STATUS_${loanRequestList.status}"/></display:column>
                        <%--<display:column property="auditUser.username" sortable="true" titleKey="loanRequest.auditUser" style="width: 7%;"/>--%>
                        <display:column sortProperty="requestDate" sortable="true" titleKey="loanRequest.requestDate" style="width: 7%;">
                            <fmt:formatDate value="${loanRequestList.requestDate}" pattern="${datePattern}"/>
                        </display:column>
                        <%--<display:column sortable="true" titleKey="loanRequest.feedbackType" style="width: 9%;"><fmt:message key="FEEDBACK_STATUS_${loanRequestList.feedbackType}"/></display:column>--%>
                        <display:column property="loanAmount" titleKey="loanRequest.loanAmount" style="width: 9%;"/>
                        <display:column property="policyFee" titleKey="loanRequest.policyFee" style="width: 9%;"/>
                        <display:column sortable="false" titleKey="loanRequestDetail.actionTitle">
                        <security:authorize ifAnyGranted="ROLE_DEALER_USER">
                            <c:if test="${loanRequestList.phase != 'draft' and loanRequestList.phase != 'dealer_submit' and loanRequestList.phase != 'bank_credit_audit'}">
                                    <a class="btn btn-primary btn-xs" href='/copyRequest?reqId=${loanRequestList.id}'><fmt:message key="loanRequestList.copy"></fmt:message></a>
                            </c:if>
                        </security:authorize>
                        </display:column>
                        <security:authorize ifAnyGranted="ROLE_ADMIN">
                            <display:column sortable="false" titleKey="loanRequestDetail.actionTitle">
                                <a class="btn btn-primary btn-xs"><span class="open-transfer" id="${loanRequestList.id}"><fmt:message key="loanRequestList.transfer"></fmt:message></span></a>
                            </display:column>
                        </security:authorize>
                        <security:authorize ifAnyGranted="ROLE_SCA">
                            <c:if test="${loanRequestList.phase == 'ca_audit'}">
                            <display:column sortable="false" titleKey="loanRequestDetail.actionTitle">
                                <a class="btn btn-primary btn-xs"><span class="open-transfer" id="${loanRequestList.id}"><fmt:message key="loanRequestList.transfer"></fmt:message></span></a>
                            </display:column>
                            </c:if>
                        </security:authorize>

                        <display:setProperty name="paging.banner.item_name"><fmt:message key="loanRequestList.loanRequest"/></display:setProperty>
                        <display:setProperty name="paging.banner.items_name"><fmt:message key="loanRequestList.loanRequests"/></display:setProperty>

                        <display:setProperty name="export.excel.filename"><fmt:message key="loanRequestList.title"/>.xls</display:setProperty>
                        <display:setProperty name="export.csv.filename"><fmt:message key="loanRequestList.title"/>.csv</display:setProperty>
                        <display:setProperty name="export.pdf.filename"><fmt:message key="loanRequestList.title"/>.pdf</display:setProperty>
                    </display:table>

                </div><!-- /.box-body -->
                <div id="transfer" title="<fmt:message key='loanRequestList.transfer'/>">
                    <form class="form-horizontal margin" method="get" action="/loanRequests/transferOperator">
                        <div class="form-group">
                            <input type="text" name="reqId" id="transfer_id" hidden/>
                            <label class="col-sm-2"><fmt:message key="loanRequest.number"/></label>
                            <div class="col-sm-10">
                                <input id="transfer_number" class="form-control" readonly/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2"><fmt:message key="loanRequestList.transfer.user"/></label>
                            <div class="col-sm-10">
                                <select class="form-control" name="auditor" id="selectUserList">
                                    <option><fmt:message key="option.select"/></option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button class="btn btn-primary" type="submit"><fmt:message key='loanRequestList.transfer'/></button>
                            </div>
                        </div>

                    </form>
                </div>
            </div><!-- /.box -->
        </div>
    </div>
</section><!-- /.content -->

<c:set var="scripts" scope="request">
    <script type='text/javascript' src="<c:url value='/dwr/interface/UserManager.js'/>"></script>
    <script type='text/javascript' src="<c:url value='/dwr/engine.js'/>"></script>
    <script type='text/javascript' src="<c:url value='/dwr/util.js'/>"></script>
<script type="text/javascript">
    $(function () {
        $('.datepicker').datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: "mm/dd/yy"
        });
        $('#loanRequestList').dataTable({
            "language": {
                "url": "//cdn.datatables.net/plug-ins/1.10.7/i18n/Chinese.json"
            },
            "bPaginate": false,
            "bLengthChange": false,
            "bFilter": false,
            "bSort": true,
            "bInfo": false,
            "bAutoWidth": true
        });

        var options = {
            autoOpen: false,
            height: 300,
            width: 450,
            modal: true
        };
        var dlg = $('#transfer').dialog(options);
        $(".open-transfer").click(function(e){
            var reqNumber = $(this).parent().parent().prevAll().eq(10).children('a').html();
            var reqId=$(this).attr("id");
            $.ajax('/loanRequests/getTransferUsers?reqId='+reqId,{
                success:function(data){
                    dwr.util.addOptions('selectUserList', data, 'value');
                }
            });
            $("#transfer_id").val(reqId);
            $("#transfer_number").val(reqNumber);
            dlg.dialog("open");

        });

    });
</script>
</c:set>
